package com.example.exceldemo.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class CreateSonarImage {
    public static void main(String[] args) {
        try {
            // 创建一个300x200的图像
            BufferedImage image = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = image.createGraphics();
            
            // 设置背景为白色
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, 300, 200);
            
            // 绘制一些文本
            g2d.setColor(Color.BLUE);
            Font font = new Font("Arial", Font.BOLD, 16);
            g2d.setFont(font);
            g2d.drawString("Sonar Analysis Example", 50, 50);
            
            // 绘制一些图表元素
            g2d.setColor(Color.GREEN);
            g2d.fillRect(50, 70, 80, 20);
            g2d.setColor(Color.RED);
            g2d.fillRect(50, 100, 40, 20);
            g2d.setColor(Color.ORANGE);
            g2d.fillRect(50, 130, 60, 20);
            
            // 添加标签
            g2d.setColor(Color.BLACK);
            g2d.setFont(new Font("Arial", Font.PLAIN, 12));
            g2d.drawString("Code Coverage: 80%", 150, 85);
            g2d.drawString("Bugs: 4", 150, 115);
            g2d.drawString("Code Smells: 6", 150, 145);
            
            g2d.dispose();
            
            // 确保目录存在
            String templateDir = "src/main/resources/template";
            File dir = new File(templateDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            
            // 保存图像
            String filePath = Paths.get(templateDir, "sonarDefault.png").toString();
            File outputFile = new File(filePath);
            ImageIO.write(image, "png", outputFile);
            
            System.out.println("Sonar image created successfully at: " + outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 